package com.msh.http.controller;

import com.alibaba.excel.EasyExcel;
import com.msh.entity.OrderCollectEntity;
import com.msh.http.viewModel.BillExportDataVo;
import com.msh.http.viewModel.SalesInfo;
import com.msh.http.viewModel.SkuCollect;
import com.msh.service.OrderCollectService;
import com.msh.service.ReportService;
import com.msh.viewmodel.BarCharCollect;
import com.msh.viewmodel.Pager;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author :zhangchuanfu
 * @Date :2023/2/3
 * @Time :14:31
 * @description :合作商订单汇总接口
 */
@RequiredArgsConstructor
@RequestMapping("/report")
@RestController
public class ReportController {

    private final ReportService reportService;
    private final OrderCollectService orderCollectService;

    /**
     * 获取一定日期范围之内的合作商分成汇总数据
     * @param pageIndex
     * @param pageSize
     * @param partnerName
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/partnerCollect")
    public Pager<OrderCollectEntity> getPartnerCollect(
            @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Long pageIndex,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10") Long pageSize,
            @RequestParam(value = "partnerName",required = false,defaultValue = "") String partnerName,
            @RequestParam(value = "start",required = true,defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
            @RequestParam(value = "end",required = true,defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
            ){
        return reportService.getPartnerCollect(pageIndex,pageSize,partnerName,start,end);
    };

    /**
     * 获取合作商前12个点位分账数据
     * @param partnerId
     * @return
     */
    @GetMapping("/top12Collect/{partnerId}")
    public List<OrderCollectEntity> getTop12(@PathVariable Integer partnerId){
        return reportService.getTop12(partnerId);
    };

    /**
     * 合作商点位分账搜索
     *
     * @param pageIndex
     * @param pageSize
     * @param partnerId
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/search/{partnerId}")
    public Pager<OrderCollectEntity> search(
            @PathVariable Integer partnerId,
            @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Long pageIndex,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10") Long pageSize,
            @RequestParam(value = "nodeName",required = false,defaultValue = "") String nodeName,
            @RequestParam(value = "start",required = true,defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
            @RequestParam(value = "end",required = true,defaultValue = "") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ) {
        return reportService.search(pageIndex,pageSize,partnerId,nodeName,start,end);
    }

    /**
     * 数据导出
     * @param partnerId
     * @param start
     * @param end
     * @param nodeName
     */
    @GetMapping("/export/{partnerId}/{start}/{end}")
    public void export(
                    HttpServletResponse response,
                   @PathVariable Integer partnerId,
                   @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd" ) LocalDate  start,
                   @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd" ) LocalDate end,
                   @RequestParam(value = "nodeName",required = false,defaultValue = "") String nodeName) throws IOException {
        var exportData = reportService.getList(partnerId,nodeName,start,end)
                .stream().map(item->{
                    BillExportDataVo billVo = new BillExportDataVo();
                    billVo.setAmount(item.getTotalBill());
                    billVo.setDate(Date.from(item.getDate().atTime(0,0).atZone(ZoneId.systemDefault()).toInstant()));
                    billVo.setOrderCount(item.getOrderCount());
                    billVo.setNodeName(item.getNodeName());
                    return billVo;
                }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition","attachment;filename=bill.xlsx");
        EasyExcel.write(response.getOutputStream(),BillExportDataVo.class).sheet("分账数据").doWrite(exportData);
    }

    /**
     * 获取合作商一定日期范围的收益情况
     * @param partnerId
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/collectReport/{partnerId}/{start}/{end}")
    public BarCharCollect getCollectReport(@PathVariable Integer partnerId,
                                           @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
                                           @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        return reportService.getCollect(partnerId,start,end);
    }

    /**
     * 获取销售额统计
     * @param collectType
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/amountCollect/{collectType}/{start}/{end}")
    public BarCharCollect getAmountCollect(@PathVariable Integer collectType,
                                           @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
                                           @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        return reportService.getAmountCollect(collectType,start,end);
    }

    /**
     * 获取销售额区域分布
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/regionCollect/{start}/{end}")
    public BarCharCollect getRegionCollect(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
                                           @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        return reportService.getCollectByRegion(start,end);
    }

    /**
     * 获取商品热榜前n
     * @param top
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/skuTop/{top}/{start}/{end}")
    public List<SkuCollect> getSkuTop(@PathVariable Integer top,
                                      @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
                                      @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        return reportService.getSkuTop(top,start,end);
    }

    /**
     * 获取一定时间内的订单量
     *
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/orderCount")
    int getOrderCount(@RequestParam(value = "partnerId",required = false) Integer partnerId,
                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime start,
                      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end,
                      @RequestParam(value = "innerCode",required = false) Integer innerCode
                      ) {
        return reportService.getOrderCount(partnerId,innerCode,start,end);
    }

    /**
     * 获取规定时间内的销售额
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/orderAmount")
    long getOrderAmount(@RequestParam(value = "partnerId",required = false) Integer partnerId,
                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end,
                        @RequestParam(value = "innerCode",required = false) Integer innerCode) {
        return reportService.getOrderAmount(partnerId,innerCode,start,end);
    }

    /**
     * 获取售货机商品销量
     * @param innerCode
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/skuCollect/{innerCode}/{start}/{end}")
    public List<SkuCollect> getSkuCollect(@PathVariable("innerCode") Integer innerCode,
                                          @PathVariable("start") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate start,
                                          @PathVariable("end") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        return reportService.getSkuCollect(innerCode,start,end);
    }

    /**
     * 获取合作商 周收入情况
     * @param partnerId
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/amountInfo/{partnerId}/{start}/{end}")
    public SalesInfo getAmountInfo(@PathVariable("partnerId") Integer partnerId,
                                   @PathVariable("start") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime start,
                                   @PathVariable("end") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end) {
        return reportService.getAmountInfo(partnerId,start,end);
    }

    /**
     * 获取合作商 周订单数情况
     * @param partnerId
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/orderCountInfo/{partnerId}/{start}/{end}")
    public SalesInfo getOrderCountInfo(@PathVariable("partnerId") Integer partnerId,
                                       @PathVariable("start") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime start,
                                       @PathVariable("end") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end) {
        return reportService.getOrderCountInfo(partnerId,start,end);
    }
    /**
     * order-service/report/totalBill
     */
    @GetMapping("/totalBill")
    public long getTotalBill(@RequestParam(value = "start") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime start,
                                                 @RequestParam("end") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end
                                                 ) {
        return reportService.getTotalBill(start,end);
    }
}
